package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.HmFamilyVo;
import org.dromara.hm.domain.bo.HmFamilyBo;
import org.dromara.hm.service.IHmFamilyService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 客户家庭组
 *
 * @author Hzy
 * @date 2025-02-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/family")
public class HmFamilyController extends BaseController {

    private final IHmFamilyService hmFamilyService;

    /**
     * 查询客户家庭组列表
     */
    @GetMapping("/list")
    public TableDataInfo<HmFamilyVo> list(HmFamilyBo bo, PageQuery pageQuery) {
        return hmFamilyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户家庭组列表
     */
    @Log(title = "客户家庭组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmFamilyBo bo, HttpServletResponse response) {
        List<HmFamilyVo> list = hmFamilyService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户家庭组", HmFamilyVo.class, response);
    }

    /**
     * 获取客户家庭组详细信息
     *
     * @param familyId 主键
     */
    @GetMapping("/{familyId}")
    public R<HmFamilyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long familyId) {
        return R.ok(hmFamilyService.queryById(familyId));
    }

    /**
     * 新增客户家庭组
     */
    @Log(title = "客户家庭组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmFamilyBo bo) {
        return toAjax(hmFamilyService.insertByBo(bo));
    }

    /**
     * 修改客户家庭组
     */
    @Log(title = "客户家庭组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmFamilyBo bo) {
        return toAjax(hmFamilyService.updateByBo(bo));
    }

    /**
     * 删除客户家庭组
     *
     * @param familyIds 主键串
     */
    @Log(title = "客户家庭组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{familyIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] familyIds) {
        return toAjax(hmFamilyService.deleteWithValidByIds(List.of(familyIds), true));
    }
}
